import os
import re
def listDir(path,extensions,reuslts = []):
    for x in os.listdir(path):
        filepath = path+"/"+x
        if os.path.isfile(filepath):
            for e in extensions:
                if x.endswith("."+e):
                    reuslts.append(filepath)
                    break
        elif os.path.isdir(filepath):
            listDir(filepath,extensions=extensions,reuslts=reuslts)
        else:
            print(f'other====:{filepath}')
    return reuslts


def listDir2(path,reuslts = []):
    for x in os.listdir(path):
        filepath = path+"/"+x
        if os.path.isdir(filepath):
            reuslts.append(filepath)
            listDir2(filepath,reuslts=reuslts)
#         else:
#             print(f'other====:{filepath}')
    return reuslts

def isInfile(filepath,imagename):
    try:
        with open(filepath) as f:
            text = f.read()
            return text.find(imagename) != -1
    except FileNotFoundError:
        print('无法打开指定的文件!')
        return False
    except LookupError:
        print('指定了未知的编码!')
        return False
    except UnicodeDecodeError:
        print(f'读取文件时解码错误!{filepath}')
        return False

    return False

def getAllImages(dirpath):
    pattern = re.compile(r'\/([^\/]+)@.*')
    sets = {""}
    for x in listDir(dirpath,extensions=["png"],reuslts = []):
        for y in re.findall(pattern, x):
            sets.add(y)
    return [x for x in sets if len(x)>0]

def printList(l):
    for x in l:
        print(x)
    
if __name__ == "__main__":
    getAllImages = getAllImages('/Users/joker/Workplace/zhengwutong3/zhengwutong/Resources/Images')
    allSourceFiles = listDir('/Users/joker/Workplace/zhengwutong3',extensions=["xib","swift","storyboard"],reuslts = [])
    
    for img in getAllImages:
        isfileIn = False
        for x in allSourceFiles:
            if isInfile(x, img):
                isfileIn = True
            if img.find("_gray") != -1:
                isfileIn = True
            if img.find("contact_status_") != -1:
                isfileIn = True
            if img.find("incoming_detail_") != -1:
                isfileIn = True
            if img.find("contact_edit_process") != -1:
                isfileIn = True
            if img.find("news_fav") != -1:
                isfileIn = True

        if isfileIn is not True:
            print(img)
    
    print("查找完毕")